import 'package:flutter/material.dart';
import '../models/heritage_activity.dart';
import '../theme/colors.dart';

/// 活动卡片组件
class ActivityCard extends StatelessWidget {
  final HeritageActivity activity;
  final VoidCallback? onTap;
  final bool showFullInfo;
  final EdgeInsetsGeometry? margin;

  const ActivityCard({
    Key? key,
    required this.activity,
    this.onTap,
    this.showFullInfo = true,
    this.margin,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: margin ?? const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
      child: Card(
        elevation: 4,
        shadowColor: AppColors.cardShadow,
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(12),
        ),
        child: InkWell(
          onTap: onTap,
          borderRadius: BorderRadius.circular(12),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              _buildImage(),
              Padding(
                padding: const EdgeInsets.all(16),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    _buildHeader(),
                    const SizedBox(height: 8),
                    _buildTitle(),
                    const SizedBox(height: 8),
                    _buildDescription(),
                    if (showFullInfo) ...[
                      const SizedBox(height: 12),
                      _buildInfo(),
                      const SizedBox(height: 12),
                      _buildFooter(),
                    ]
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  Widget _buildImage() {
    return ClipRRect(
      borderRadius: const BorderRadius.vertical(top: Radius.circular(12)),
      child: AspectRatio(
        aspectRatio: 16 / 9,
        child: Image.network(
          activity.imageUrl,
          fit: BoxFit.cover,
          errorBuilder: (context, error, stackTrace) {
            return Container(
              color: AppColors.border,
              child: const Center(
                child: Icon(
                  Icons.image_not_supported,
                  size: 48,
                  color: AppColors.textHint,
                ),
              ),
            );
          },
          loadingBuilder: (context, child, loadingProgress) {
            if (loadingProgress == null) return child;
            return Container(
              color: AppColors.background,
              child: Center(
                child: CircularProgressIndicator(
                  value: loadingProgress.expectedTotalBytes != null
                      ? loadingProgress.cumulativeBytesLoaded /
                          loadingProgress.expectedTotalBytes!
                      : null,
                  color: AppColors.primary,
                ),
              ),
            );
          },
        ),
      ),
    );
  }

  Widget _buildHeader() {
    return Row(
      children: [
        _buildCategoryChip(),
        const SizedBox(width: 8),
        _buildStatusChip(),
        const Spacer(),
        if (activity.isFeatured)
          const Icon(
            Icons.star,
            size: 16,
            color: AppColors.secondary,
          ),
      ],
    );
  }

  Widget _buildCategoryChip() {
    final category = activity.category;
    Color chipColor;
    String categoryText;
    
    switch (category) {
      case 'workshop':
        chipColor = Colors.blue;
        categoryText = '工作坊';
        break;
      case 'exhibition':
        chipColor = Colors.green;
        categoryText = '展览';
        break;
      case 'performance':
        chipColor = Colors.purple;
        categoryText = '演出';
        break;
      case 'lecture':
        chipColor = Colors.orange;
        categoryText = '讲座';
        break;
      case 'festival':
        chipColor = Colors.red;
        categoryText = '节庆';
        break;
      case 'competition':
        chipColor = Colors.teal;
        categoryText = '比赛';
        break;
      default:
        chipColor = Colors.grey;
        categoryText = '其他';
        break;
    }
    
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
      decoration: BoxDecoration(
        color: chipColor,
        borderRadius: BorderRadius.circular(12),
      ),
      child: Text(
        categoryText,
        style: const TextStyle(
          color: Colors.white,
          fontSize: 12,
          fontWeight: FontWeight.w500,
        ),
      ),
    );
  }

  Widget _buildStatusChip() {
    final status = activity.status;
    Color chipColor;
    String statusText;
    
    switch (status) {
      case ActivityStatus.upcoming:
        chipColor = Colors.green;
        statusText = '即将开始';
        break;
      case ActivityStatus.ongoing:
        chipColor = Colors.blue;
        statusText = '进行中';
        break;
      case ActivityStatus.completed:
        chipColor = Colors.grey;
        statusText = '已结束';
        break;
      case ActivityStatus.cancelled:
        chipColor = Colors.red;
        statusText = '已取消';
        break;
      case ActivityStatus.suspended:
        chipColor = Colors.orange;
        statusText = '暂停';
        break;
      case ActivityStatus.full:
        chipColor = Colors.amber;
        statusText = '已满员';
        break;
    }
    
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
      decoration: BoxDecoration(
        color: chipColor.withOpacity(0.1),
        border: Border.all(color: chipColor, width: 1),
        borderRadius: BorderRadius.circular(12),
      ),
      child: Text(
        statusText,
        style: TextStyle(
          color: chipColor,
          fontSize: 12,
          fontWeight: FontWeight.w500,
        ),
      ),
    );
  }

  Widget _buildTitle() {
    return Text(
      activity.title,
      style: const TextStyle(
        fontSize: 18,
        fontWeight: FontWeight.bold,
        color: AppColors.textPrimary,
      ),
      maxLines: 2,
      overflow: TextOverflow.ellipsis,
    );
  }

  Widget _buildDescription() {
    return Text(
      activity.description,
      style: const TextStyle(
        fontSize: 14,
        color: AppColors.textSecondary,
        height: 1.4,
      ),
      maxLines: showFullInfo ? 3 : 2,
      overflow: TextOverflow.ellipsis,
    );
  }

  Widget _buildInfo() {
    return Column(
      children: [
        _buildInfoRow(
          Icons.access_time,
          '时间',
          _formatDateTime(activity.startDate),
        ),
        const SizedBox(height: 8),
        _buildInfoRow(
          Icons.location_on,
          '地点',
          activity.location,
        ),
        const SizedBox(height: 8),
        _buildInfoRow(
          Icons.people,
          '参与人数',
          '${activity.currentParticipants}/${activity.maxParticipants}',
        ),
      ],
    );
  }

  Widget _buildInfoRow(IconData icon, String label, String value) {
    return Row(
      children: [
        Icon(
          icon,
          size: 16,
          color: AppColors.textSecondary,
        ),
        const SizedBox(width: 4),
        Text(
          '$label：',
          style: const TextStyle(
            fontSize: 12,
            color: AppColors.textSecondary,
          ),
        ),
        Expanded(
          child: Text(
            value,
            style: const TextStyle(
              fontSize: 12,
              color: AppColors.textPrimary,
            ),
            maxLines: 1,
            overflow: TextOverflow.ellipsis,
          ),
        ),
      ],
    );
  }

  Widget _buildFooter() {
    return Row(
      children: [
        if (activity.price > 0)
          Text(
            '￥${activity.price.toStringAsFixed(0)}',
            style: const TextStyle(
              fontSize: 16,
              fontWeight: FontWeight.bold,
              color: AppColors.error,
            ),
          )
        else
          const Text(
            '免费',
            style: TextStyle(
              fontSize: 16,
              fontWeight: FontWeight.bold,
              color: AppColors.success,
            ),
          ),
        const Spacer(),
        if (activity.rating > 0) ...[
          Icon(
            Icons.star,
            size: 16,
            color: Colors.amber[600],
          ),
          const SizedBox(width: 4),
          Text(
            activity.rating.toStringAsFixed(1),
            style: const TextStyle(
              fontSize: 12,
              color: AppColors.textSecondary,
            ),
          ),
          const SizedBox(width: 4),
          Text(
            '(${activity.reviewCount})',
            style: const TextStyle(
              fontSize: 12,
              color: AppColors.textHint,
            ),
          ),
        ]
      ],
    );
  }

  String _formatDateTime(DateTime dateTime) {
    return '${dateTime.month}月${dateTime.day}日 ${dateTime.hour.toString().padLeft(2, '0')}:${dateTime.minute.toString().padLeft(2, '0')}';
  }
}

/// 简化版活动卡片
class SimpleActivityCard extends StatelessWidget {
  final HeritageActivity activity;
  final VoidCallback? onTap;

  const SimpleActivityCard({
    Key? key,
    required this.activity,
    this.onTap,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ActivityCard(
      activity: activity,
      onTap: onTap,
      showFullInfo: false,
      margin: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
    );
  }
}